package com.szcinda.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.szcinda.controller.configuration.UserLoginToken;
import com.szcinda.repository.BaoGuanFee;
import com.szcinda.repository.BaoGuanFeeRepository;
import com.szcinda.service.PageResult;
import com.szcinda.service.dto.carrier.*;
import com.szcinda.service.dto.client.ClientVehiclePriceDto;
import com.szcinda.service.dto.line.VehicleFeeQueryDto;
import com.szcinda.service.dto.vehicle.VehicleExcelListener;
import com.szcinda.service.dto.vehicle.VehicleImportDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("carrier")
public class CarrierController {

    private final CarrierService carrierService;
    private final BaoGuanFeeRepository baoGuanFeeRepository;

    public CarrierController(CarrierService carrierService, BaoGuanFeeRepository baoGuanFeeRepository) {
        this.carrierService = carrierService;
        this.baoGuanFeeRepository = baoGuanFeeRepository;
    }


    @UserLoginToken
    @PostMapping("create")
    @OperationLogAnnotation(operDesc = "创建供应商", operModel = "供应商管理")
    public Result<String> create(@RequestBody CarrierCreateDto createDto) {
        carrierService.create(createDto);
        return Result.success();
    }

    @UserLoginToken
    @PostMapping("modify")
    @OperationLogAnnotation(operDesc = "修改供应商", operModel = "供应商管理")
    public Result<String> update(@RequestBody CarrierUpdateDto updateDto) {
        carrierService.update(updateDto);
        return Result.success();
    }

    @GetMapping("queryByOrgId/{orgId}")
    public List<CarrierDto> queryByOrgId(@PathVariable String orgId){
        return carrierService.queryByOrgId(orgId);
    }

    @PostMapping("query")
    public PageResult<CarrierDto> query(@RequestBody CarrierQueryDto queryDto) {
        return carrierService.query(queryDto);
    }

    @GetMapping("del/{id}/{userId}")
    @OperationLogAnnotation(operDesc = "删除供应商", operModel = "供应商管理")
    public Result<String> del(@PathVariable String id, @PathVariable String userId) {
        carrierService.delete(id, userId);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result<CarrierDetailDto> GetById(@PathVariable String id) {
        return Result.success(carrierService.getById(id));
    }

    @GetMapping("all")
    public Result<List<CarrierDto>> all(){
        return Result.success(carrierService.getAll());
    }

    @GetMapping("baoGuanFee/{clientId}")
    public Result<BaoGuanFee> baoGuanFee(@PathVariable String clientId) {
        return Result.success(baoGuanFeeRepository.findByBelongId(clientId));
    }

    @PostMapping("vehicleFees")
    public Result<List<ClientVehiclePriceDto>> vehicleFees(@RequestBody VehicleFeeQueryDto vehicleFeeQueryDto) {
        return Result.success(carrierService.getVehiclePrices(vehicleFeeQueryDto));
    }

    @PostMapping("/import/{opId}")
    @OperationLogAnnotation(operDesc = "导入供应商", operModel = "供应商管理")
    public Result<String> orderImport(@RequestParam("file") MultipartFile file, @PathVariable String opId) throws Exception {
        // 解析每行结果在listener中处理
        InputStream inputStream = file.getInputStream();
        try {
            CarrierExcelListener listener = new CarrierExcelListener();
            EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1, CarrierImportDto.class), listener);
            carrierService.batchCreate(listener.getImportDatas(), opId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/importVehicle/{belongId}")
    @OperationLogAnnotation(operDesc = "修改供应商车型费用", operModel = "供应商管理")
    public Result<String> importVehicle(@RequestParam("file") MultipartFile file, @PathVariable String belongId) throws Exception {
        // 解析每行结果在listener中处理
        InputStream inputStream = file.getInputStream();
        try {
            CarrierVehicleFeeExcelListener listener = new CarrierVehicleFeeExcelListener();
            EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1, CarrierVehicleFeeImportDto.class), listener);
            carrierService.batchCreateVehicleList(listener.getImportDatas(), belongId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
